<template>
  <div class="rbt-cart-page">
    <div class="container">
      <RbtBread>
        <RbtBreadItem to="/">首页</RbtBreadItem>
        <RbtBreadItem>购物车</RbtBreadItem>
      </RbtBread>
      <div class="cart">
        <table>
          <thead>
            <tr>
              <th width="120">
                <RbtCheckout :modelValue="cart.isCheckAll" @change="checkAll"
                  >全选</RbtCheckout
                >
              </th>
              <th width="400">商品信息</th>
              <th width="220">单价</th>
              <th width="180">数量</th>
              <th width="180">小计</th>
              <th width="140">操作</th>
            </tr>
          </thead>
          <!-- 有效商品 -->
          <tbody>
            <tr v-if="cart.validList.length === 0">
              <td colspan="6">
                <CartNone />
              </td>
            </tr>
            <tr v-for="item in cart.validList" :key="item.skuId">
              <td>
                <RbtCheckout
                  :modelValue="item.selected"
                  @change="checkOne(item, $event)"
                />
              </td>
              <td>
                <div class="goods">
                  <RouterLink :to="`/product/${item.id}`"
                    ><img :src="item.picture"
                  /></RouterLink>
                  <div>
                    <p class="name ellipsis">
                      {{ item.name }}
                    </p>
                    <!-- 选择规格组件 -->
                    <CartSku
                      :attrsText="item.attrsText"
                      :skuId="item.skuId"
                      @change="($event: SkuType) => updateCartSku(item.skuId, $event)"
                    />
                  </div>
                </div>
              </td>
              <td class="tc">
                <p>&yen;{{ item.nowPrice }}</p>
                <p v-if="item.price as number - item.nowPrice > 0">
                  比加入时降价
                  <span class="red"
                    >&yen;{{ (item.price as number) - item.nowPrice }}</span
                  >
                </p>
              </td>
              <td class="tc">
                <RbtNumbox
                  :modelValue="item.count"
                  :max="item.stock"
                  @change="changeCount(item, $event)"
                />
              </td>
              <td class="tc">
                <p class="f16 red">
                  &yen;{{ (item.nowPrice * 100 * item.count) / 100 }}
                </p>
              </td>
              <td class="tc">
                <p><a href="javascript:;">移入收藏夹</a></p>
                <p>
                  <a
                    href="javascript:;"
                    class="green"
                    @click="deleteCart(item.skuId)"
                    >删除</a
                  >
                </p>
                <p><a href="javascript:;">找相似</a></p>
              </td>
            </tr>
          </tbody>
          <!-- 无效商品 -->
          <tbody v-if="cart.invalidList.length">
            <tr>
              <td colspan="6"><h3 class="tit">失效商品</h3></td>
            </tr>
            <tr v-for="item in cart.invalidList" :key="item.skuId">
              <td><RbtCheckout style="color: #eee" /></td>
              <td>
                <div class="goods">
                  <RouterLink :to="`/product/${item.id}`"
                    ><img :src="item.picture"
                  /></RouterLink>
                  <div>
                    <p class="name ellipsis">
                      {{ item.name }}
                    </p>
                    <p class="attr">{{ item.attrsText }}</p>
                  </div>
                </div>
              </td>
              <td class="tc">
                <p>&yen;{{ item.nowPrice }}</p>
              </td>
              <td class="tc">{{ item.count }}</td>
              <td class="tc">
                <p>
                  &yen;{{
                    (Math.round(item.nowPrice * 100) * item.count) / 100
                  }}
                </p>
              </td>
              <td class="tc">
                <p>
                  <a
                    class="green"
                    href="javascript:;"
                    @click="deleteCart(item.skuId)"
                    >删除</a
                  >
                </p>
                <p><a href="javascript:;">找相似</a></p>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <!-- 操作栏 -->
      <div class="action">
        <div class="batch">
          <RbtCheckout :modelValue="cart.isCheckAll" @change="checkAll"
            >全选</RbtCheckout
          >
          <a href="javascript:;" @click="batchDeleteCart(false)">删除商品</a>
          <a href="javascript:;">移入收藏夹</a>
          <a href="javascript:;" @click="batchDeleteCart(true)">清空失效商品</a>
        </div>
        <div class="total">
          共 {{ cart.validTotal }} 件商品，已选择
          {{ cart.selectTotal }} 件，商品合计：
          <span class="red">¥{{ cart.selectedAmount }}</span>
          <RbtButton type="primary" @click="goCheckout">下单结算</RbtButton>
        </div>
      </div>
      <!-- 猜你喜欢 -->
      <GoodRelevant />
    </div>
  </div>
</template>

<script setup lang="ts">
import Confirm from '@/components/Confirm'
import Message from '@/components/Message'
import useStore from '@/store'
import { CartType } from '@/types/cart'
import { SkuType } from '@/types/goods'
import { useRouter } from 'vue-router'
const { cart, user } = useStore()

// 单选
const checkOne = (item: CartType, selected: boolean) => {
  cart.updateCartAsync({ ...item, selected })
}
// 全选
const checkAll = (selected: boolean) => {
  cart.checkAllCart(selected)
}

// 删除
const deleteCart = (skuId: string) => {
  Confirm({ text: '您确定从购物车删除改商品吗?', title: '提示' })
    .then(() => {
      cart.deleteCartAsync(skuId)
    })
    .catch(() => {
      console.log('点击取消')
    })
}

// 批量删除
const batchDeleteCart = (isClear: boolean) => {
  Confirm({
    text: `您确定从购物车删除${isClear ? '失效' : '选中'}的商品吗?`,
    title: '提示'
  }).then(() => {
    cart.batchDeleteCart(isClear)
  })
}

// 修改数量
const changeCount = (item: CartType, count: number) => {
  cart.updateCartAsync({ ...item, count })
}

// 修改规格
const updateCartSku = (oldSkuId: string, newSku: SkuType) => {
  cart.updateCartSku(oldSkuId, newSku)
}

// 跳转结算页面
const router = useRouter()
const goCheckout = () => {
  // 1.判断是否选择有效商品
  if (cart.selectTotal === 0)
    return Message({ type: 'warn', text: '至少选中一件商品' })
  // 2.判断是否已经登录 为登录 弹窗提示
  if (!user.user.token) {
    Confirm({ text: '下单结算需要登录,您是否去登录?', title: '提示' }).then(
      () => {
        // 点击确定
        router.push('/member/checkout')
      }
    )
  } else {
    router.push('/member/checkout')
  }
}
</script>

<style scoped lang="scss">
.tc {
  text-align: center;
  .rbt-numbox {
    margin: 0 auto;
    width: 120px;
  }
}
.red {
  color: $priceColor;
}
.green {
  color: $rbtColor;
}
.f16 {
  font-size: 16px;
}
.goods {
  display: flex;
  align-items: center;
  img {
    width: 100px;
    height: 100px;
  }
  > div {
    width: 280px;
    font-size: 16px;
    padding-left: 10px;
    .attr {
      font-size: 14px;
      color: #999;
    }
  }
}
.action {
  display: flex;
  background: #fff;
  margin-top: 20px;
  height: 80px;
  align-items: center;
  font-size: 16px;
  justify-content: space-between;
  padding: 0 30px;
  .rbt-checkout {
    color: #999;
  }
  .batch {
    a {
      margin-left: 20px;
    }
  }
  .red {
    font-size: 18px;
    margin-right: 20px;
    font-weight: bold;
  }
}
.tit {
  color: #666;
  font-size: 16px;
  font-weight: normal;
  line-height: 50px;
}

.rbt-cart-page {
  .cart {
    background: #fff;
    color: #666;
    table {
      border-spacing: 0;
      border-collapse: collapse;
      line-height: 24px;
      th,
      td {
        padding: 10px;
        border-bottom: 1px solid #f5f5f5;
        &:first-child {
          text-align: left;
          padding-left: 30px;
          color: #999;
        }
      }
      th {
        font-size: 16px;
        font-weight: normal;
        line-height: 50px;
      }
    }
  }
}
</style>
